package com.cjw.service.impl.personal;

import com.cjw.enums.FileType;
import com.cjw.enums.share.CommentType;
import com.cjw.enums.share.DeleteRemark;
import com.cjw.enums.share.SharingType;
import com.cjw.enums.share.TagType;
import com.cjw.mapper.livesquare.LiveSharingMapper;
import com.cjw.mapper.livesquare.LiveUserMapper;
import com.cjw.mapper.soulread.ReadMapper;
import com.cjw.mapper.soulread.ReadMinioMapper;
import com.cjw.mapper.soulread.ReadUserMapper;
import com.cjw.pojo.dto.database.PageDTO;
import com.cjw.pojo.dto.database.UserDTO;
import com.cjw.pojo.dto.livesquare.LiveSharingDTO;
import com.cjw.pojo.dto.livesquare.MyLiveShareDTO;
import com.cjw.pojo.query.livesquare.QueryAllDTO;
import com.cjw.pojo.query.person.AddReadDTO;
import com.cjw.pojo.dto.soulread.MyReadDTO;
import com.cjw.pojo.entity.livesquare.LiveSharing;
import com.cjw.pojo.entity.livesquare.LiveUser;
import com.cjw.pojo.entity.soulread.Read;
import com.cjw.pojo.entity.soulread.ReadMinio;
import com.cjw.pojo.entity.soulread.ReadUser;
import com.cjw.service.database.MinioService;
import com.cjw.service.livesquare.CommentService;
import com.cjw.service.person.MyDesignService;
import com.cjw.service.soulread.HeatService;
import com.cjw.service.soulread.TagService;
import com.cjw.util.BaseContextHolder;
import com.cjw.util.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @create_time: 2021.12.19
 * @author : cjw
 */
@Service("myDesignService")
public class MyDesignServiceImpl implements MyDesignService {
    @Autowired
    private TagService tagService;

    @Autowired
    private MinioService minioService;

    @Autowired
    private LiveSharingMapper liveSharingMapper;

    @Autowired
    private LiveUserMapper liveUserMapper;

    @Autowired
    private ReadMapper readMapper;

    @Autowired
    private HeatService heatService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private ReadUserMapper readUserMapper;

    @Autowired
    private ReadMinioMapper readMinioMapper;

    @Override
    public List<MyLiveShareDTO> findAllLiveSharing(Integer page, Integer size) {
        QueryAllDTO queryAllDTO = new QueryAllDTO();
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        queryAllDTO.setUserId(user.getId());
        queryAllDTO.setBegin(page*size);
        queryAllDTO.setSize(size);
        LiveUser liveUser = new LiveUser();
        liveUser.setUserId(user.getId());
        List<MyLiveShareDTO> myLiveShareDTOs = new ArrayList<>();
        for (LiveSharingDTO liveSharingDTO : liveSharingMapper.queryAllLiveDTO(queryAllDTO)) {
            for (LiveUser liveUser1 : liveUserMapper.select(liveUser)) {
                if (liveSharingDTO.getId() == liveUser1.getLiveId()) {
                    if (liveUser1.getType() == 1) {
                        liveSharingDTO.setLove(true);
                    } else {
                        liveSharingDTO.setCollection(true);
                    }
                }
            }
            liveSharingDTO.setLove(liveSharingDTO.getLove() == null ? false : liveSharingDTO.getLove());
            liveSharingDTO.setCollection(liveSharingDTO.getCollection() == null ? false : liveSharingDTO.getCollection());
            MyLiveShareDTO copy = BeanUtils.copy(liveSharingDTO, MyLiveShareDTO.class);
            LiveSharing liveSharing = new LiveSharing();
            liveSharing.setId(copy.getId());
            int length = copy.getContext().length() > 20 ? 20 : copy.getContext().length();
            String substring = copy.getContext().substring(0,length);
            copy.setContext(substring);
            copy.setPower(liveSharingMapper.selectOne(liveSharing).getPower());
            myLiveShareDTOs.add(copy);
        }
        return myLiveShareDTOs;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean OpenClosePowerLive(Long liveId) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        LiveSharing liveSharing = new LiveSharing();
        liveSharing.setId(liveId);
        liveSharing.setUserId(user.getId());
        LiveSharing liveSharing1 = liveSharingMapper.selectOne(liveSharing);
        if (liveSharing1 == null)
            return false;
        liveSharing.setPower(liveSharing1.getPower() == 0 ? 1 : 0);
        return liveSharingMapper.updateByPrimaryKeySelective(liveSharing)==1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteLive(Long liveId) {
        LiveSharing liveSharing = new LiveSharing();
        liveSharing.setId(liveId);
        return liveSharingMapper.deleteByPrimaryKey(liveSharing)==1;
    }

    @Override
    public PageDTO<List<MyReadDTO>> findAllMyReadDTO(Integer page, Integer size, String find) {
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        PageDTO<List<MyReadDTO>> reads = new PageDTO<>();
        ArrayList<MyReadDTO> myReadDTOS = new ArrayList<>();
        for (Read read : readMapper.findAllMyRead(page * size, size, find, user.getId())) {
            MyReadDTO copy = BeanUtils.copy(read, MyReadDTO.class);
            copy.setHeat(heatService.getHeat(copy.getId()).intValue());
            copy.setCommentNum(commentService.getCommentNum(copy.getId(), CommentType.READ.getCode()));
            ReadUser readUser = new ReadUser();
            readUser.setReadId(copy.getId());
            readUser.setType(SharingType.LOVE.getCode());
            copy.setLoveNum(readUserMapper.selectCount(readUser));
            myReadDTOS.add(copy);
        }
        reads.setDtos(myReadDTOS);
        Read read = new Read();
        read.setUserId(user.getId());
        reads.setTotal(readMapper.selectCount(read));
        return reads;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRead(Long readId) {
        Read read = new Read();
        read.setId(readId);
        read.setDeleteRemark(DeleteRemark.DELETE.getCode());
        return readMapper.updateByPrimaryKeySelective(read) == 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean changePowerRead(Long readId) {
        Read read = new Read();
        read.setId(readId);
        read.setIsPublic(readMapper.selectOne(read).getIsPublic() == 1 ? 0 : 1);
        return readMapper.updateByPrimaryKeySelective(read) == 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addRead(AddReadDTO addReadDTO) {
        Read read = addDTOToRead(addReadDTO);
        readMapper.insertUseGeneratedKeys(read);
        ReadMinio readMinio = new ReadMinio();
        readMinio.setReadId(read.getId());
        readMinio.setOssId(minioService.getIdByUrl(addReadDTO.getReadUrl()));
        readMinioMapper.insert(readMinio);
        for (String tag : addReadDTO.getTagNames()) {
            tagService.addTagRead(read.getId(), tag, TagType.EMPTY_LABEL.getMsg());
        }
        return read.getId();
    }

    private Read addDTOToRead(AddReadDTO addReadDTO) {
        Read read = new Read();
        UserDTO user = (UserDTO) BaseContextHolder.get("user");
        read.setTopic(addReadDTO.getTopic());
        read.setContent(addReadDTO.getContent());
        read.setUserId(user.getId());
        read.setPicture(addReadDTO.getPicture());
        read.setIsPublic(addReadDTO.getIsPublic());
        read.setAddTime(new Date());
        read.setDeleteRemark(DeleteRemark.UNDELETE.getCode());
        read.setScore(0.00);
        return read;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String upReadImage(MultipartFile image) {
        return minioService.MinIOUpload(image, FileType.IMAGE.getCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String upReadVideo(MultipartFile video) {
        return minioService.MinIOUpload(video, FileType.VIDEO.getCode());
    }
}
